# AIDE 生产环境部署脚本 - PowerShell版本
# 使用方法：在项目根目录运行 .\deploy-production.ps1

Write-Host "🚀 开始部署AIDE生产环境..." -ForegroundColor Green

# 检查必要的环境变量文件
if (-not (Test-Path ".env.prod")) {
    Write-Host "❌ 错误：未找到 .env.prod 文件" -ForegroundColor Red
    Write-Host "请先创建 .env.prod 文件并配置以下变量：" -ForegroundColor Yellow
    Write-Host "DB_PASSWORD=your_secure_db_password"
    Write-Host "JWT_SECRET=your_super_secret_jwt_key"
    Write-Host "OPENAI_API_KEY=your_openai_api_key"
    Write-Host "REDIS_PASSWORD=your_redis_password"
    Write-Host "CORS_ORIGIN=https://yourdomain.com"
    Write-Host "SMTP_HOST=your_smtp_host"
    Write-Host "SMTP_PORT=587"
    Write-Host "SMTP_USER=your_smtp_user"
    Write-Host "SMTP_PASS=your_smtp_password"
    exit 1
}

# 创建必要的目录
Write-Host "📁 创建数据存储目录..." -ForegroundColor Blue
New-Item -ItemType Directory -Force -Path ".\logs" | Out-Null
New-Item -ItemType Directory -Force -Path ".\ssl" | Out-Null
New-Item -ItemType Directory -Force -Path ".\nginx\logs" | Out-Null

# 停止现有容器（如果存在）
Write-Host "🛑 停止现有容器..." -ForegroundColor Yellow
docker-compose -f docker-compose.prod.yml -f docker-compose.windows.yml --env-file .env.prod down

# 清理旧的镜像（可选）
Write-Host "🧹 清理旧镜像..." -ForegroundColor Cyan
docker image prune -f

# 构建并启动服务
Write-Host "🏗️ 构建并启动服务（Windows环境）..." -ForegroundColor Green
docker-compose -f docker-compose.prod.yml -f docker-compose.windows.yml --env-file .env.prod up --build -d

# 等待服务启动
Write-Host "⏳ 等待服务启动..." -ForegroundColor Yellow
Start-Sleep -Seconds 30

# 检查服务状态
Write-Host "🔍 检查服务状态..." -ForegroundColor Blue
docker-compose -f docker-compose.prod.yml -f docker-compose.windows.yml --env-file .env.prod ps

# 显示部署结果
Write-Host "✅ 部署完成！" -ForegroundColor Green
Write-Host "🌐 访问地址: http://your-server-ip" -ForegroundColor Cyan
Write-Host "📊 查看日志: docker-compose -f docker-compose.prod.yml logs -f" -ForegroundColor Cyan
Write-Host "🔧 进入后端容器: docker-compose -f docker-compose.prod.yml exec backend sh" -ForegroundColor Cyan
Write-Host "🔧 进入前端容器: docker-compose -f docker-compose.prod.yml exec frontend sh" -ForegroundColor Cyan
